package kr.saeildamil.androidstudy_second26;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class EnglishWord extends Activity {
	WordDBHelper mHelper;
	EditText mText;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch26_englishword);
		mHelper=new WordDBHelper(this);
		mText=(EditText) findViewById(R.id.edittext);
	}
	public void mOnClick(View v){
		SQLiteDatabase db;
		ContentValues row;
		
		switch (v.getId()) {
		case R.id.insert:
			db=mHelper.getWritableDatabase();
			//dbhelper에서 지원하는 메소드를 이용
			row=new ContentValues();
			row.put("eng", "boy");
			row.put("han", "머시마");
			
			db.insert("dic", null, row);
			
			//SQL문으로 삽입
			String sql="insert into dic values(null,'girl','가시나');";
			db.execSQL(sql);
			
			mText.setText("Insert Success");
			break;
			
		case R.id.delete:
			db=mHelper.getWritableDatabase();
			db.delete("dic", null, null);
			//SQL문장으로 하려면 db.execSQL("delete from dic");
			mText.setText("Delete Success");
			mHelper.close();
			
			break;
			
		case R.id.update:
			db=mHelper.getWritableDatabase();
			
			row=new ContentValues();
			row.put("han", "소년");
			db.update("dic", row, "end='boy'", null);
			sql="update dic set han='소년' where eng='girl';";
			db.execSQL(sql);
			mHelper.close();
			mText.setText("Update Success");
			break;

		case R.id.select:
			db=mHelper.getReadableDatabase();
			Cursor cursor;
			cursor=db.query("dic", new String[]{"_id","eng", "han"}, null, null, null, null, null);
			//cursor=db.rawQuery("select _id, eng, han from dic",null);
			
			StringBuilder sb=new StringBuilder();
			
			sb.append("컬럼수:"+cursor.getColumnCount()+"\n");
			sb.append("결과레코드"+cursor.getColumnCount()+"\n");
			
			while (cursor.moveToNext()) {
				sb.append(cursor.getInt(0)+":");
				sb.append(cursor.getString(1)+"=");
				sb.append(cursor.getString(2)+"\n");
			}
			if(sb.length()==0){
				mText.setText("Empty Set");
			}else{
				mText.setText(sb.toString());
			}
			mHelper.close();
			break;
		}
	}
	
}
